﻿using CSRedis;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QskjLibrary
{
    public static class HttpHelper
    {
        public static CSRedisClient cSRedisClient = null;
        /// <summary>
        /// 出租车基础信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> Get()
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://117.185.15.54:48083/getCar?startPos=0&endPos=9000");
            request.Headers.Add("userId", "admin");
            request.Headers.Add("userKey", "admin");
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 出租车驾驶员基础信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetDriver()
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://117.185.15.54:48083/getDriver?startPos=0&endPos=50000");
            request.Headers.Add("userId", "admin");
            request.Headers.Add("userKey", "admin");
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取token
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtToken()
        {
            //if (cSRedisClient == null)
            //{
            //    cSRedisClient = RedisHelp.GetInstance();
            //}
            //string ztToken = cSRedisClient.Get("ztToken");
            //if (ztToken == null)
            //{
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getToken");
            var content = new StringContent("{\r\n    \"appAccount\": \"videoUser\",\r\n    \"appPassword\": \"videoUser@2023\"\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            //cSRedisClient.Set("ztToken", res, new TimeSpan(6, 0, 0));
            return res;
            //}
            //else
            //{
            //    return ztToken;
            //}
        }
        /// <summary>
        /// 中台获取驾驶员信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtDriver(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"09509ff833db44e0a044b0a9c28faa38\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 50000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取出租车驾驶员排班信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtCarDriver(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"565ab4456e2b48babb2b5860329233e8\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 500000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取出租车信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtCar(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"5ed79a00c48c4e899eeacb4cd421f5cb\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 50000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取组织架构信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtOrg(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"de0320fa232945df97d9a2dc7313717b\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 100000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中安科技获取大巴车信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZakjBus()
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://ec.china317.com:6385/restServer?code=qs&timestamp=20231220144209&signature=DCC9EFF08FCB155DFCD6935D92ABEA4D&action=getDevices&password=qsdbc@123&version=2.00&userName=qsdbc&pageSize=10000&pageNo=1");
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取大巴车租赁信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtBusLeaseRecord(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"ffc5091f8135470da8bc07f34bf9a111\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 10000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取大巴车驾驶员信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtBusDriver(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"09509ff833db44e0a044b0a9c28faa38\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 100000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取大巴车驾驶员绑定信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetBusDriverBind(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"565ab4456e2b48babb2b5860329233e8\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 1000000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 中台获取大巴车信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetZtBus(string token)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.61:8081/api/dataservice/apiService/getData");
            request.Headers.Add("Authorization", $@"{token}");
            var content = new StringContent("{\r\n    \"apiCode\": \"5ed79a00c48c4e899eeacb4cd421f5cb\",\r\n    \"parmList\": {\r\n        \"parm1\": \"value\"\r\n    },\r\n    \"current\": 1,\r\n    \"size\": 1000000\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 查询webRtcToken
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="user">杨旭平台用户</param>
        /// <param name="password">杨旭平台密码</param>
        /// <returns></returns>
        public async static Task<string> GetWebRtcToken(string url, string user, string password)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            var content = new StringContent("{\"userName\":\"" + user + "\",\"password\":\"" + password + "\"}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 获取下载信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetDownloadVideoId(string token, string url, string deviceId, string dts, string dte, int scale, string strWater, string userName)
        {
            string qskj = string.IsNullOrWhiteSpace(strWater) ? userName : strWater;
            qskj = qskj.Replace(" ", "");
            Console.WriteLine(qskj);
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            request.Headers.Add("Authorization", token);
            var content = new StringContent("{\r\n    \"deviceID\": \"" + deviceId + "\",\r\n    \"endTime\": \"" + dte + "\",\r\n    \"watermark\": \"" + qskj + "\",\r\n    \"scale\": " + scale + ",\r\n    \"startTime\": \"" + dts + "\"\r\n}", null, "application/json");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 获取下载信息
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetDownloadVideoUrl(string token, string url, string deviceId, string playId)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Get, $@"{url}?deviceID={deviceId}&playID={playId}");
            request.Headers.Add("Authorization", token);
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
        /// <summary>
        /// 查询token生成国标码操作
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetToken()
        {
            try
            {
                var client = new HttpClient();
                var request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.3.231:40001/api/v1/Qs_User/UserToken");
                var content = new StringContent("{\r\n    \"loginName\": \"lvzhanchao\",\r\n    \"password\": \"B8C1707506F0BF38C743BAE0E8217DE3\"\r\n}", null, "application/json");
                request.Content = content;
                var response = await client.SendAsync(request);
                response.EnsureSuccessStatusCode();
                string res = await response.Content.ReadAsStringAsync();
                Console.WriteLine(res);
                return res;
            }
            catch (Exception e)
            {
                Console.WriteLine($@"查询token生成国标码操作处理异常：" + e.Message + JsonConvert.SerializeObject(e));
                return "";
            }
        }
        /// <summary>
        /// 生成国标码操作0
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetProtectDeviceCode0(string token)
        {

            try
            {
                var client = new HttpClient();
                var request = new HttpRequestMessage(HttpMethod.Post, $@"http://192.168.3.231:40001/api/v1/Qs_Device/DeviceProductCode?model=CAR&mainSub=0");
                request.Headers.Add("Authorization", "Bearer " + token);
                var response = await client.SendAsync(request);
                response.EnsureSuccessStatusCode();
                string res = await response.Content.ReadAsStringAsync();
                return res;
            }
            catch (Exception e)
            {
                Console.WriteLine($@"生成国标码操作0操作处理异常：" + e.Message + JsonConvert.SerializeObject(e));
                return "";
            }
        }
        /// <summary>
        /// 生成国标码操作1
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task<string> GetProtectDeviceCode1(string token)
        {
            try
            {
                var client = new HttpClient();
                var request = new HttpRequestMessage(HttpMethod.Post, $@"http://192.168.3.231:40001/api/v1/Qs_Device/DeviceProductCode?model=CAR&mainSub=1");
                request.Headers.Add("Authorization", "Bearer " + token);
                var response = await client.SendAsync(request);
                response.EnsureSuccessStatusCode();
                string res = await response.Content.ReadAsStringAsync();
                return res;
            }
            catch (Exception e)
            {
                Console.WriteLine($@"生成国标码操作1操作处理异常：" + e.Message + JsonConvert.SerializeObject(e));
                return "";
            }
        }
        /// <summary>
        /// 查询稽查证据视图库地址
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetGetFileUrl(string token, string key, string pid)
        {

            try
            {
                var client = new HttpClient();
                var request = new HttpRequestMessage(HttpMethod.Get, $@"http://192.168.3.231:40001/api/v1/Qs_NetworkInspector/GetFileUrl?key={key}&pid={pid}");
                request.Headers.Add("Authorization", "Bearer " + token);
                var response = await client.SendAsync(request);
                response.EnsureSuccessStatusCode();
                string res = await response.Content.ReadAsStringAsync();
                return res;
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}
